{extend name="shared/layout" /}
{block name="title"}图片分组{/block}
{block name="css"}{/block}
{block name="javascript"}
    <script type="text/javascript">
        function initTable() {
            //先销毁表格
            $('#tb').bootstrapTable('destroy');
            //初始化表格,动态从服务器加载数据
            $("#tb").bootstrapTable({
                method: "Post",  //使用Post请求到服务器获取数据
                url: "{:url('image/getgroupList')}", //获取数据的Servlet地址
                striped: true,  //表格显示条纹
                pagination: true, //启动分页
                pageSize: 20,  //每页显示的记录数
                pageNumber: 1, //当前第几页
                pageList: [20, 50],  //记录数可选列表
                search: false,  //是否启用查询
                sortable: true, //是否启用排序
                sortOrder: "asc", //排序方式                
                sortable: true,                
                sidePagination: "server", //表示服务端请求                
                queryParamsType: "limit",
                contentType: "application/x-www-form-urlencoded",
                queryParams: function queryParams(params) {   //设置查询参数
                    var param = {
                        limit: params.limit,
                        offset: params.offset,
                        sortName: params.sort,
                        sortOrder: params.order,
                        keyword: $("input[name=keyword]").val()                      
                    };
                    return param;
                },
                onLoadSuccess: function () {  //加载成功时执行
                    $('.show_image').hover(
                        function(){
                            $(this).next().css('display','block');
                        },
                        function(){
                            $(this).next().css('display','none');
                        }
                    );
                },
                onLoadError: function () {  //加载失败时执行
                    layui.use(['layer'], function(){
                        layer.msg("加载数据失败", { time: 1500, icon: 2 });
                    })
                },
                rowStyle: function (row, index) {
                    var strclass = "";
                    if (row.isdisable==1) {
                        strclass = 'danger';
                    }                
                    else {
                        return {};
                    }
                    return { classes: strclass }
                }
            });
        }
        
        $(document).ready(function () {
            //调用函数，初始化表格
            initTable();
            //当点击查询按钮的时候执行
            $("#search").bind("click", initTable);
            $("#btnAdd").bind("click", add);
        });

        var layer_add_index;
        function add() {
            layui.use(['layer'], function(){
                layer_add_index = layer.open({
                    type: 2,
                    title: "新增分组",
                    shadeClose: true,
                    shade: false,
                    area:['100%', '100%'],
                    content:["{:url('image/addgroup')}","no"]
                })
            });
        }

        var layer_edit_index;
        function edit(id) {
            layui.use(['layer'], function(){
                layer_edit_index = layer.open({
                    type: 2,
                    title: "修改分组",
                    shadeClose: true,
                    shade: false,
                    area:['100%', '100%'],
                    content:["{:url('image/editgroup')}?id="+id,"no"]
                })
            });
        }

        function operateFormatter(index, row) {
            var html = "";
            html += "<a href=\"javascript:edit(" + row.id + ");\"><i class=\"fa fa-edit text-navy\"></i>修改</a>";           
            html += "&nbsp;&nbsp;<a href=\"javascript:del(" + row.id + ");\"><i class=\"fa fa-trash-o text-danger\"></i>删除</a>";           
            return html;
        }

        function imageFormatter(index, row) {            
            return "<img src='/"+index+"' width='40px' />";
        }

        function del(id) {
            layui.use(['layer'], function(){
                layer.confirm('你确定要删除该分组吗？', {
                    btn: ['是', '否'] //按钮
                }, function () {
                    var load_index = layer.load(0, { shade: false });
                    $.ajax({
                        type: "post",
                        url: "{:url('image/delgroup')}",
                        data: { id: id },
                        dataType: "json",
                        success: function (result) {
                            layer.close(load_index);
                            if (result.code == 0) {
                                var ly = layer.alert('删除成功！', function () {
                                    initTable();
                                    layer.close(ly);
                                });
                            } else {
                                layer.alert(result.msg);
                            }
                        }
                    });
                });
            });
        }       

        Date.prototype.format = function(format) {
            var date = {
                    "M+": this.getMonth() + 1,
                    "d+": this.getDate(),
                    "h+": this.getHours(),
                    "m+": this.getMinutes(),
                    "s+": this.getSeconds(),
                    "q+": Math.floor((this.getMonth() + 3) / 3),
                    "S+": this.getMilliseconds()
            };
            if (/(y+)/i.test(format)) {
                    format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            for (var k in date) {
                    if (new RegExp("(" + k + ")").test(format)) {
                            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                    }
            }
            return format;
        }

        function dateFormat(index, row) {
            if(index==0) return "";
           var newDate = new Date();
            newDate.setTime(index * 1000);
            return newDate.format('yyyy-MM-dd h:m:s');
        }

        function rownumber(value, row, index){
            var options = $('#tb').bootstrapTable('getOptions');            
            return options.pageSize * (options.pageNumber - 1) + index + 1;
        }        
    </script>
{/block}

{block name="content"}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>图片分组</h5>
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                            <div class="col-sm-7 m-b-xs">
                                <span style="padding-right:20px;">
                                    <button type="button" class="btn btn-primary" id="btnAdd">新增</button>
                                </span>
                            </div>
                            <div class="col-sm-2">
                                <div class="input-group">
                                    <span class="input-group-btn">
                                        <input type="text" placeholder="请输入关键词" class="input-sm form-control" id="keyword" name="keyword">
                                        <button type="button" class="btn btn-sm btn-primary" id="search">搜索</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <table data-toggle="table" id="tb" data-pagination="true">
                            <thead>
                                <tr>
                                    <th data-field="number" data-align="center" data-formatter="rownumber">序号</th>
                                    <th data-field="name" data-align="center">分组名称</th>                                    
                                    <th data-field="addtime" data-align="center" data-formatter="dateFormat">创建时间</th>
                                    <th data-align="center" data-formatter="operateFormatter">操作</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
{/block}